package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Collection; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import java.util.List;

import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblDKMHCamHoacQuyDinh;
import com.lb.jwitter.service.TblDKMHCamHoacQuyDinhService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHCamHoacQuyDinhInsertAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private Long id; 
    public void setId(Long id) {
       this.id = id;
 }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
 }

    public int getDotDKId() {
       return dotDKId;
 }

    private TblDKMHDotDKService tblDKMHDotDKService; 

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private TblThongTinHocVienService tblThongTinHocVienService; 

    private int monHocId; 
    public void setMonHocId(int monHocId) {
       this.monHocId = monHocId;
 }

    public int getMonHocId() {
       return monHocId;
 }

    private TblCTDTMonHocService tblCTDTMonHocService; 

    private String nhomHoc; 
    public void setNhomHoc(String nhomHoc) {
       this.nhomHoc = nhomHoc;
 }

    public String getNhomHoc() {
       return nhomHoc;
 }

    private String maTinhNang; 
    public void setMaTinhNang(String maTinhNang) {
       this.maTinhNang = maTinhNang;
 }

    public String getMaTinhNang() {
       return maTinhNang;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private TblCanBoService tblCanBoService; 

    private String ngayCapNhat; 
    public void setNgayCapNhat(String ngayCapNhat) {
       this.ngayCapNhat = ngayCapNhat;
 }

    public String getNgayCapNhat() {
       return ngayCapNhat;
 }

    private TblDKMHCamHoacQuyDinh tblDKMHCamHoacQuyDinh; 
    public TblDKMHCamHoacQuyDinh  getTblDKMHCamHoacQuyDinh() {
        return tblDKMHCamHoacQuyDinh;
 }

    private TblDKMHCamHoacQuyDinhService tblDKMHCamHoacQuyDinhService; 

	private int preInsert(Message msg, TblDKMHCamHoacQuyDinh obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblDKMHCamHoacQuyDinh obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
	
	private String maMonHoc;
	public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}

	private String mssv;
  public String getMssv() {
		return mssv;
	}

	public void setMssv(String mssv) {
		this.mssv = mssv;
	}

private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_CamHoacQuyDinh", "them")){ 
        return "noprivilege"; 
    } 
        tblDKMHCamHoacQuyDinhService = (TblDKMHCamHoacQuyDinhService) context.getBean("tbl_DKMH_CamHoacQuyDinhClient"); 
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 

        tblThongTinHocVienService = (TblThongTinHocVienService) context.getBean("tbl_ThongTin_HocVienClient"); 

        tblCTDTMonHocService = (TblCTDTMonHocService) context.getBean("tbl_CTDT_MonHocClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        msg = new Message(); 
        tblDKMHCamHoacQuyDinh = new TblDKMHCamHoacQuyDinh(); 
        tblDKMHCamHoacQuyDinh.setDotDKId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId))); 
        
        
        
        //get mon hoc theo ma mon hoc
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        {
        	ConditionReport con = new ConditionReport();
        	con.setCol("ma");
        	con.setValue1(maMonHoc);
        	con.setOperator("eq");
        	cons.add(con);
        }
        
        List<TblCTDTMonHoc> monHoc = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 1, 1);
        
        if(monHoc == null || monHoc.size() <1){
        	return Action.ERROR;
        }
        tblDKMHCamHoacQuyDinh.setMonHocId(monHoc.get(0));
        
        cons.clear();
        {
        	ConditionReport con = new ConditionReport();
        	con.setCol("MSSV");
        	con.setValue1(mssv);
        	con.setOperator("eq");
        	cons.add(con);
        }
        List<TblThongTinHocVien> hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
        if(hocVien == null || hocVien.size() <1){
        	return Action.ERROR;
        }
        tblDKMHCamHoacQuyDinh.setHocVienId(hocVien.get(0));
        
        tblDKMHCamHoacQuyDinh.setNhomHoc(nhomHoc); 
        tblDKMHCamHoacQuyDinh.setMaTinhNang(maTinhNang); 
        tblDKMHCamHoacQuyDinh.setCanBoId(tblCanBoService.gettblCanBo(new Long(canBoId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date now = new Date();
       ngayCapNhat = now.toString();
       
       Date dd = now;//df.parse(ngayCapNhat); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHCamHoacQuyDinh.setNgayCapNhat(d); 
        preInsert(msg,tblDKMHCamHoacQuyDinh); 
       if (msg.isPreMessage_b()) 
       { 
           id = tblDKMHCamHoacQuyDinhService.createtblDKMHCamHoacQuyDinh(tblDKMHCamHoacQuyDinh); 
           postInsert(msg, tblDKMHCamHoacQuyDinh); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
       tblDKMHCamHoacQuyDinh.setId(id); 
       if(pageSize == 0){ 
           pageSize = 20; 
       } 
        if(page == 0){ 
           page =1; 
       } 
      dataString = ""; 
      dataString += "&id="; 
      dataString += "&dotDKId="; 
      dataString += "&hocVienId="; 
      dataString += "&monHocId="; 
      dataString += "&nhomHoc="; 
      dataString += "&maTinhNang="; 
      dataString += "&canBoId="; 
      dataString += "&ngayCapNhat="; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_DKMH_CamHoacQuyDinh Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
